/**
 * 时间日期处理类
 * @version 1.0.0
 */
class DateUtils {
    dateInfo = {}
    date = null;
    constructor() {
        this.parse();
    }
    parse(dateStr) {
        this.date = dateStr ? new Date(dateStr) : new Date();
        if (isNaN(this.date.getFullYear())) {
            this.date = new Date();
        }
        let info = {
            Y: this.date.getFullYear(),
            m: ('0' + (this.date.getMonth() + 1)).slice(-2),
            d: ('0' + this.date.getDate()).slice(-2),
            H: ('0' + this.date.getHours()).slice(-2),
            i: ('0' + this.date.getMinutes()).slice(-2),
            s: ('0' + this.date.getSeconds()).slice(-2),
            w: this.date.getDay(),
            ww: ['日', '一', '二', '三', '四', '五', '六'][this.date.getDay()],
        }
        this.dateInfo = Object.keys(info)
            .sort((a, b) => b.length - a.length)
            .reduce((obj, key) => {
                obj[key] = info[key];
                return obj;
            }, {});
        return this;
    }
    format(fmtStr) {
        for (let k in this.dateInfo) {
            fmtStr = fmtStr.replace(new RegExp(`(${k})`, 'ig'), this.dateInfo[k]);
        }
        return fmtStr;
    }
}

export default DateUtils;